package com.changgou.search.controller;

import com.changgou.entity.Page;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;


@Controller
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String, String> searchMap) {
        //特殊符号处理(url编码转换 可以省略)
        this.handleSearchMap(searchMap);
        Map searchResult = searchService.search(searchMap);
        return searchResult;
    }

    /**
     * url编码字符集替换  +  替换成  %2B
     *
     * @param searchMap
     */
    private void handleSearchMap(Map<String, String> searchMap) {
        Set<Map.Entry<String, String>> entries = searchMap.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            //将路径中的+ 替换成%2B 进行转换
            searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
        }
    }


    /**
     * 查询完毕后,组装查询条件和返回结果用于生成静态页面
     *
     * @param searchMap
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(@RequestParam Map<String, String> searchMap, Model model) {
        //获取查询结果
        Map resultMap = searchService.search(searchMap);
        //特殊符号处理(url编码转换)
        this.handleSearchMap(searchMap);
        //添加ES中查询的结果集到对象模型中
        model.addAttribute("result", resultMap);
        //添加查询条件到模型中
        model.addAttribute("searchMap", searchMap);

        //参数一: 总记录数
        //参数二: 总页数
        //参数三:指定分页默认长度
        Page<SkuInfo> page = new Page<SkuInfo>(
                Long.parseLong(String.valueOf(resultMap.get("total"))),
                Integer.parseInt((String) resultMap.get("pageNum")),
                Page.pageSize
        );

        //添加分页对象
        model.addAttribute("page", page);

        //拼装url
        StringBuilder url = new StringBuilder("/search/list");
        if (searchMap != null && searchMap.size() > 0) {
            //有查询条件,则进行url参数拼装  /search/list?keywords=手机&brand=华为
            url.append("?");
            for (String paramKey : searchMap.keySet()) {
                //分页信息和排序信息需要重置,则不进行拼接
                if (!"sortRule".equals(paramKey) && !"sortField".equals(paramKey) && !"pageNum".equals(paramKey)){
                    //    /search/list?keywords=手机&brand=华为
                    url.append(paramKey).append("=").append(searchMap.get(paramKey)).append("&");
                }
                //将拼接后的StringBuilder 转为String字符串
                String urlString = url.toString();
                //拼接完成后,需要去除最后一个&字符
                urlString = urlString.substring(0, urlString.length() - 1);
                //将拼接后的url地址保存到数据模型中
                model.addAttribute("url", urlString);
            }
        } else {
            //如果没有查询条件,则不进行拼接直接返回Url
            model.addAttribute("url", url);
        }
        return "search";
    }
}
